package br.com.naybee.client.views.add.configuracao;

import br.com.naybee.client.NaybeeService;
import br.com.naybee.client.views.add.menu.configuracao.MenuConfiguracaoView;
import br.com.naybee.client.views.base.Principal;
import br.com.naybee.framework.client.componente.gwt.event.CommEvent;
import br.com.naybee.framework.client.componente.gwt.event.CommHandler;
import br.com.naybee.framework.client.componente.gwt.web.Div;
import br.com.naybee.framework.client.componente.gwt.web.InputText;
import br.com.naybee.framework.client.componente.gwt.web.Option;
import br.com.naybee.framework.client.componente.gwt.web.SelectMultiple;
import br.com.naybee.framework.client.models.ListModel;
import br.com.naybee.framework.client.models.Model;
import br.com.naybee.framework.client.models.Models;
import br.com.naybee.framework.client.utils.NAYException;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class CadastrarConfiguracaoView extends Composite {

	private static CadastrarConfiguracaoUiBinder uiBinder = GWT.create(CadastrarConfiguracaoUiBinder.class);

	interface CadastrarConfiguracaoUiBinder extends UiBinder<Widget, CadastrarConfiguracaoView> {
	}

	@UiField
	protected Div divPortas;
	@UiField
	protected Div divRepartimento;
	@UiField
	protected Div divTipoDeAutomacao;
	@UiField
	protected InputText descricao;

	private SelectMultiple selectMultipleRepartimento = new SelectMultiple();
	private SelectMultiple selectMultipleTipoDeAutomacao = new SelectMultiple();
	private SelectMultiple selectMultiplePorta = new SelectMultiple();

	public CadastrarConfiguracaoView() {
		initWidget(uiBinder.createAndBindUi(this));
		init();
	}

	private void init() {
		selectMultipleRepartimento.setStyleName("select-div");
		selectMultipleTipoDeAutomacao.setStyleName("select-div");
		selectMultiplePorta.setStyleName("select-div");
		initRepartimento();
		initPorta();
		initTipoAutomacao();
	}

	private void initRepartimento() {

		NaybeeService naybeeService = new NaybeeService("repartimentoFs", "listarRepartimentos");
		naybeeService.post(null, new CommHandler() {
			public void onSuccess(CommEvent event) {
				carregarRepartimento(event.getListModel());
			}

			public void onException(NAYException error) {
			}
		});
	}

	private void initPorta() {
		NaybeeService naybeeService = new NaybeeService("portaFs", "listarPortas");
		naybeeService.post(null, new CommHandler() {
			public void onSuccess(CommEvent event) {
				carregarPorta(event.getListModel());
			}

			public void onException(NAYException error) {
			}
		});
	}

	private void initTipoAutomacao() {
		NaybeeService naybeeService = new NaybeeService("tipoAutomacaoFs", "listarTipoAutomacao");
		naybeeService.post(null, new CommHandler() {
			public void onSuccess(CommEvent event) {
				carregarTipoDeAutomacao(event.getListModel());
			}

			public void onException(NAYException error) {
			}
		});
	}

	private void carregarRepartimento(ListModel listModel) {
		for (Model model : listModel) {
			Option option = new Option();
			option.setClassId(model.getStr("idRepartimento"));
			option.setText(model.getStr("descricaoRepartimento").toLowerCase());
			selectMultipleRepartimento.add(option);
			divRepartimento.add(selectMultipleRepartimento);
		}
	}

	private void carregarPorta(ListModel listModel) {
		for (Model model : listModel) {
			Option option = new Option();
			option.setClassId(model.getStr("idPorta"));
			option.setText(model.getStr("descricaoPorta"));
			selectMultiplePorta.add(option);
			divPortas.add(selectMultiplePorta);
		}
	}

	private void carregarTipoDeAutomacao(ListModel listModel) {
		for (Model model : listModel) {
			Option option = new Option();
			option.setClassId(model.getStr("idAutomacao"));
			option.setText(model.getStr("descricaoAutomacao"));
			selectMultipleTipoDeAutomacao.add(option);
			divTipoDeAutomacao.add(selectMultipleTipoDeAutomacao);
		}
	}

	@UiHandler("btnSalvar")
	public void onClickBtnSalvar(ClickEvent event) {
		String idRepartimento = selectMultipleRepartimento.getOption().getId();
		String idTipoDeAutomacao = selectMultipleTipoDeAutomacao.getOption().getId();
		String idPorta = selectMultiplePorta.getOption().getId();
		if (validarCampos()) {
			Model model = Models.newModel();
			model.add("descricaoConfiguracao", descricao.getValue());
			model.add("idRepartimento", idRepartimento);
			model.add("idAutomacao", idTipoDeAutomacao);
			model.add("idPorta", idPorta);
			salvar(model);
		} else {
			Window.alert("Preecha todos os campos");
		}
	}

	private void salvar(Model model) {
		NaybeeService naybeeService = new NaybeeService("configuracaoFs", "registrarConfiguracao");
		naybeeService.post(model, new CommHandler() {
			public void onSuccess(CommEvent event) {
				Principal.get().add(new MenuConfiguracaoView());
			}

			public void onException(NAYException error) {
			}
		});
	}

	private boolean validarCampos() {
		if (descricao.getValue().equals("")) {
			return false;
		} else if (!selectMultipleRepartimento.isSelect()) {
			return false;
		} else if (!selectMultipleTipoDeAutomacao.isSelect()) {
			return false;
		} else if (!selectMultiplePorta.isSelect()) {
			return false;
		}
		return true;
	}

}
